/**
 * Created by lwy on 2017/8/25.
 */

var ue = UE.getEditor('container', {
    autoHeight: false,
    initialFrameHeight:800
});
function getContent() {
    var html = ue.getContent();
    return html;
}

//判断是否登录
$(function(){
    var token = localStorage.getItem('crmtoken');
    if(token==""||token==null||token==undefined){
        window.location.href="/crm/login.jsp";
    }
})

//上传主图
var mainImg;
$('#avatarInput').on('change', function(e) {
    var filemaxsize = 1024 * 5;//5M
    var target = $(e.target);
    var Size = target[0].files[0].size / 1024;
    if(Size > filemaxsize) {
        alert('图片过大，请重新选择!');
        $(".avatar-wrapper").childre().remove;
        return false;
    }
    if(!this.files[0].type.match(/image.*/)) {
        alert('请选择正确的图片!')
    } else {
        var filename = document.querySelector("#avatar-name");
        var texts = document.querySelector("#avatarInput").value;
        var teststr = texts; //你这里的路径写错了
        testend = teststr.match(/[^\\]+\.[^\(]+/i); //直接完整文件名的
        filename.innerHTML = testend;
    }

});



$(".avatar-save").on("click", function() {
    var img_lg = document.getElementById('imageHead');
    // 截图小的显示框内的内容
    html2canvas(img_lg, {
        allowTaint: true,
        taintTest: false,
        onrendered: function(canvas) {
            canvas.id = "mycanvas";
            //生成base64图片数据
            var dataUrl = canvas.toDataURL("image/jpeg");
            var newImg = document.createElement("img");
            newImg.src = dataUrl;
            imagesAjax(dataUrl)
        }
    });
})

function imagesAjax(src) {
    var data = {};
    data.img = src;
    data.jid = $('#jid').val();
    mainImg = src;

    $.ajax({
        url:'/crm/image/uploadImage.json',
        data:{
            filed:mainImg,
        },
        dataType:'json',
        type:'post',
        success:function(data){
            if(data.code==1){
                $('#mainImg').show();
                $('#mainImg').attr('src',data.datas.path);
                $('#update').html('更换')
            }
        }
    })
}

var imgs="";
var IMG=0;
var IMG2=0;
var yuanImg = '';
function changeImg(){
    selectOver2();
}

function selectOver2(){
    IMG2++;
    $.ajaxFileUpload({
        url:'/crm/uploadImage.do?isDetail=true',
        type:'POST',
        fileElementId:'imageUpload',
        dataType: 'JSON',
        success: function (data) {
            var results=data.split('|');
            var flag=results[0].split('>');
            if(flag[1] != 'success'){
                mui.alert('上传失败');
                IMG2--;
                return;
            }
            var img=results[1].substring(results[1].length-6,0);
            if(yuanImg==""){
                yuanImg = img;
            }else{
                yuanImg = yuanImg+','+img;
            }

            var imgLength = yuanImg.split(',');
            var imgHtml = '';
            for(var i=0;i<imgLength.length;i++){
                imgHtml+='<img src="'+imgLength[i]+'" alt="img" style="width: 100%"/>'
            }
            $('#target').val(imgHtml);
            $('#content').html(imgHtml);



        },
        error:function(data){
            IMG2--;
            mui.alert('上传失败');

        }
    })
}

//保存修改
$('#save').click(function(){
    if($('#avatar-name').html()==""){
        alert('请选择图片');
    }else{
        $('#save').attr('data-dismiss','modal')
    }
})


$('#apply').click(function(){
    var mainImg ='';
    $("#mainImageAll img").each(function(i){
   mainImg+=$(this).attr("src")
    });
    if(mainImg=="../static/img/uploadImg.png"){
       mainImg='';
    }
    $.ajax({
        url:'/crm/keFuC/add.json',
        dataType:'json',
        data:{
            weChatNumber:$('#goodsName').val(),
           weChatCode:mainImg,
            token:localStorage.getItem('crmtoken'),
        },
        type:'post',
        success:function(data){
            if(data.code==0){
                alert(data.msg);
            }
            if(data.code==-1){
                window.parent.location.href="/crm/login.jsp"
            }
            if(data.code==1){
                alert('添加成功')
                window.location.href="/crm/keFuC/listView"
            }
        }
    })
})


$('#scope').focus(function(){
    $('#scopeText').show();
})

